package indi.tshoiasc.simstr.utils;

import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

public class ItemOperate {
	@SuppressWarnings("deprecation")
	public static ItemStack generateItem(int ID, String name, List<String> lore, Integer Amount) {
		ItemStack temp = new ItemStack(ID);
		ItemMeta itm = temp.getItemMeta();
		itm.setLore(lore);
		itm.setDisplayName(name);
		temp.setItemMeta(itm);
		temp.setAmount(Amount.intValue());
		return temp;
	}

	public static void setEnchant(ItemStack im, Enchantment ench, int level) {
		if (im.getEnchantmentLevel(ench) == 0) {
			if (level != 0) {
				im.addUnsafeEnchantment(ench, level);
			}
		} else if (level != 0) {
			im.removeEnchantment(ench);
			im.addUnsafeEnchantment(ench, level);
		} else {
			im.removeEnchantment(ench);
		}
	}

	public static int lookupLoreIndex(ItemStack item, String lore) {
		List<String> loretext = item.getItemMeta().getLore();
		if (loretext == null) {
			return -1;
		}
		int i = 0;
		for (String s : loretext) {
			if (s.contains(lore)) {
				break;
			}
			i++;
		}
		return i == loretext.size() ? -1 : i;
	}

	public static int getLoreIndex(ItemStack item, String lore) {
		int i = 0;
		List<String> lores = item.getItemMeta().getLore();
		for (String a : lores) {
			if (a.equalsIgnoreCase(lore)) {
				return i;
			}
			i++;
		}
		return -1;
	}

	public static void setLore(ItemStack is, int Line, String lore) {
		List<String> lores = new ArrayList<String>();
		if ((is == null) || (is.getType() == Material.AIR)) {
			throw new NullPointerException();
		}
		if (!is.hasItemMeta()) {
			return;
		}
		if (is.getItemMeta().hasLore()) {
			lores.addAll(is.getItemMeta().getLore());
			if ((lore.equals("")) || (lore == null)) {
				lores.remove(Line);
			} else {
				lores.set(Line, lore.replaceAll("&", "§"));
			}
			ItemMeta itm = is.getItemMeta();
			itm.setLore(lores);
			is.setItemMeta(itm);
		}
	}

	public static ItemStack addLore(ItemStack is, String lore) {
		if (is != null) {
			lore = ChatColor.translateAlternateColorCodes('&', lore);
			ItemMeta im = is.getItemMeta();
			if (im.hasLore()) {
				List<String> l = im.getLore();
				l.add(lore);
				im.setLore(l);
				is.setItemMeta(im);
				return is;
			}
			List<String> l = new ArrayList<String>();
			l.add(lore);
			im.setLore(l);
			is.setItemMeta(im);
			return is;
		}
		throw new NullPointerException();
	}

	public static ItemStack replaceLore(ItemStack is, String old, String newString) {
		if (is == null) {
			throw new NullPointerException();
		}
		ItemMeta im = is.getItemMeta();
		List<String> lore = im.getLore();
		if (!lore.contains(old)) {
			return is;
		}
		while (lore.contains(old)) {
			lore.set(lore.indexOf(old), newString);
		}
		im.setLore(lore);
		is.setItemMeta(im);
		return is;
	}
}
